################################################################################
# The STARTUP CLASS
#
#   A Graphical User Interface application
#
#   The first thing that started up.
#
#   The Startup class will show the last opened notes on the window (maybe?)
#
#   The Startup Class provides options to:
#
#       Create new notes file
#       Create new directory (A folder for the notes)
#       Open exiting notes file
#       Move existing notes to other locations
#       Delete existing notes
#       Settings options
#       Shutdown Device  (Main shutdown?)
################################################################################

from Tkinter import *
from SettingsClass import *
from NoteManagerClass import *
from NotesClass import *
from tkFileDialog import *
import tkMessageBox
import os
    #My notes, New Notes, Manage, Settings
#To be filled in with functionalities
class StartupClass(Frame):

    def __init__(self, master):
        self.root = master
        self.frame = Frame.__init__(self, master, background='light gray')
        self.grid(sticky=N + S + E + W)
        self.loadedData={"name":"student name", "fonts":"Lucida Handwriting", "fg":"black","bg":"light gray", "path":"/media/NOTEBOOK"}

        self.iconList=[]
        self.lblList=[]
        self.createWidgets()
        
                
    def manageNotes(self, event):
        notesWindow = Toplevel(self.root)
        notesWindow.geometry("1024x550+0+0")
        self.child = NoteManagerClass(notesWindow, self, self.loadedData["path"])

        self.root.withdraw()
        self.wait_window(notesWindow)
        self.root.deiconify()

    def openNewNotes(self, event):
        self.notesWindow = Toplevel(self.root)
        self.notesWindow.geometry("1024x480+0+0")
        self.childNote = NotesClass(self.notesWindow, self, self.loadedData["path"])
        
        self.root.withdraw()
        self.wait_window(self.notesWindow)
        self.root.deiconify()
        

    def infoHandler(self,event):
        tkMessageBox.showinfo(
            "Information","Innovative Notes is created by: \n\nElectronic Notebook Team\nQuynh Tran, Rafael Reynoso, Sureeporn Srikasem, Nazra Waheed")

    #Create a new child frame - Settings Class
    def createSettingsWindow(self, event):
        self.settingWindow = Toplevel(self.root)
        self.settingWindow.geometry("1024x590+0+0")
        SettingsClass(self.settingWindow, self, self.loadedData)
        self.root.withdraw()
        self.wait_window(self.settingWindow)
        self.root.deiconify()
        try:
            file = open("settings.txt", "r")
            self.loadedData = pickle.load(open("settings.txt", "rb"))
        except (IOError, NameError):
            pass

        try:
            self.changeFonts(self.loadedData["fonts"])
            self.changeFGcolor(self.loadedData["fg"])
            self.changeBGcolor(self.loadedData["bg"])
        except(TclError):
            pass

        
    #Display the help window
    def quitHandler(self, event):
       self.root.destroy()
        
    def createWidgets(self):

        try:
            file = open("settings.txt", "r")
            self.loadedData = pickle.load(open("settings.txt", "rb"))
        except (IOError, NameError):
            self.loadedData={"name":"student name", "fonts":"Lucida Handwriting", "fg":"black","bg":"light gray", "path":"/media/NOTEBOOK"}

        myFg=self.loadedData["fg"]
        myBg=self.loadedData["bg"]
        myfonts = tkFont.Font(family=self.loadedData["fonts"], size=16, slant="italic")
        
        self.config(bg=myBg)
        self.root.config(bg=myBg)
        
        self.space1 = Label(self, pady='40', padx='220', bg=myBg, fg=myFg)
        self.space1.grid(row=0, column=0, columnspan=9)
        self.lblList.append(self.space1)
                
        self.nameLbl = Label(self, text="Innovative Notes",foreground=myFg, bg=myBg, font=(self.loadedData["fonts"], 45, 'bold', 'underline'),
        anchor=CENTER, pady='10', padx='220')
        self.nameLbl.grid(row=1, column=0, columnspan=9)
       
        self.mottoLbl = Label(self, text="\"Truly Innovative, Truly Noteworthy\"",
            font=myfonts, foreground=myFg,bg=myBg, pady='8', padx='170')
        self.mottoLbl.grid(row=2, column=0, columnspan=10)
        self.lblList.append(self.mottoLbl)

        self.space2 = Label(self, pady='40', padx='200', bg=myBg,fg=myFg)
        self.space2.grid(row=3, column=0, columnspan=9)
        self.lblList.append(self.space2)

        self.folder = PhotoImage(file="icons/folder.gif")
        self.label = Label(self, image=self.folder, bg=myBg)
        self.label.image = self.folder
        self.label.bind("<Button-1>", self.manageNotes)
        self.label.grid(row=4, column=2)
        self.iconList.append(self.label)

        self.new = PhotoImage(file="icons/File-add-icon.gif")
        self.newNotes = Label(self, image=self.new, bg=myBg)
        self.newNotes.image = self.new
        self.newNotes.bind("<Button-1>", self.openNewNotes)
        self.newNotes.grid(row=4, column=3)
        self.iconList.append(self.newNotes)

        self.folder_add = PhotoImage(file="icons/info.gif")
        self.newFolder = Label(self, image=self.folder_add, bg=myBg)
        self.newFolder.image = self.folder_add
        self.newFolder.bind("<Button-1>", self.infoHandler)
        self.newFolder.grid(row=4, column=4)
        self.iconList.append(self.newFolder)

       

        self.settingPic = PhotoImage(file="icons/process.gif")
        self.settings = Label(self, image=self.settingPic,  bg=myBg)
        self.settings.image = self.settingPic
        self.settings.bind("<Button-1>", self.createSettingsWindow)
        self.settings.grid(row=4, column=5)
        self.iconList.append(self.settings)

        self.pageEdit = PhotoImage(file="icons/Login-out-icon.gif")
        self.manageNotes = Label(self, image=self.pageEdit,  bg=myBg)
        self.manageNotes.image = self.pageEdit
        self.manageNotes.bind("<Button-1>", self.quitHandler)
        self.manageNotes.grid(row=4, column=6)
        self.iconList.append(self.manageNotes)

        self.myNotesLbl = Label(self, text="my notes", font=myfonts,bg=myBg, fg=myFg)
        self.myNotesLbl.grid(row=5, column=2)
        self.lblList.append(self.myNotesLbl)

        self.newPgLbl = Label(self, text="new notes", font=myfonts,bg=myBg, fg=myFg)
        self.newPgLbl.grid(row=5, column=3)
        self.lblList.append(self.newPgLbl)

        self.newFolderLbl = Label(self, text="info", font=myfonts,bg=myBg,fg=myFg)
        self.newFolderLbl.grid(row=5, column=4)
        self.lblList.append(self.newFolderLbl)

        self.settingsLbl = Label(self, text="settings", font=myfonts,bg=myBg,fg=myFg)
        self.settingsLbl.grid(row=5, column=5)
        self.lblList.append(self.settingsLbl)
        
        self.manageLbl = Label(self, text="quit", font=myfonts,bg=myBg,fg=myFg)
        self.manageLbl.grid(row=5, column=6)
        self.lblList.append(self.manageLbl)


        self.space = Label(self,
            font=(self.loadedData["fonts"], 10, 'italic'),width=30, height=5, bg=myBg,fg=myFg)
        self.space.grid(row=6, column=0, columnspan=10)
        self.iconList.append(self.space)

    def changeFonts(self, font):
        self.nameLbl.config(font=(font, 45, 'bold', 'underline'))
        self.space.config(font=(font, 10, 'italic'))
        for lbl in self.lblList:
            lbl.config(font=(font, 16, 'italic'))

    def changeFGcolor(self, color):

        self.space.config(fg=color)
        self.nameLbl.config(fg=color)
        for lbl in self.lblList:
            lbl.config(fg=color)

    def changeBGcolor(self, chosenColor):
        self.nameLbl.config(background=chosenColor)
        self.config(background=chosenColor)
        self.root.config(background=chosenColor)
        for icon in self.iconList:
            icon.config(background=chosenColor)
        for lbl in self.lblList:
            lbl.config(background=chosenColor)


        
                
                
                

  